/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AdminOperations;

import java.io.File;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
 *
 * @author ABDELMOHSEN
 */
public class AdminManageDataBase {
    public void backup(String fileName){
            try {
                File f=new File("abdelmohsenback.bat");
                PrintWriter out = new PrintWriter(f);
                out.println("@echo off");
                out.println("mysqldump -u root -p1234 forum >"+fileName+".sql");
                out.print("exit");
                out.close();
                Runtime.getRuntime().exec("cmd /c start abdelmohsenback.bat");

            } catch (Exception ex) {
                Logger.getLogger(AdminManageTheme.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    public void restore(String fileName){
            try {
                File f=new File("abdelmohsenback.bat");
                PrintWriter out = new PrintWriter(f);
                out.println("@echo off");
                out.println("mysql -u root -p1234 forum <"+fileName+".sql");
                out.print("exit");
                out.close();
                Runtime.getRuntime().exec("cmd /c start abdelmohsenback.bat");

            } catch (Exception ex) {
                Logger.getLogger(AdminManageTheme.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    public Vector<String> getAvailableBackups(){
         File directory = new File("./");
         File[] files = directory.listFiles();
         Vector<String> sqls=new Vector<String>();
         String name=null;
         for(int i=0;i<files.length;i++){
             name=files[i].getName();
             if(name.endsWith(".sql"))
                 sqls.add(name);
         }
         return sqls;
    }
    
    public void SchemaExport(){
        Configuration cfg = new Configuration().configure();
        SchemaExport schemaExport = new SchemaExport(cfg);
        schemaExport.setOutputFile("ahmedSchemaExport.txt");
        schemaExport.create(false, true);
    }

    /*public static void main(String[] args){
        //new AdminManageDataBase().SchemaExport();
        //new AdminManageDataBase().restore("11");
        Vector<String> v=new AdminManageDataBase().getAvailableBackups();
        System.out.println("+++++++++"+v.size()+v.get(0));
    }*/
}
